//
//  CFAnotationView.m
//  cafe
//
//  Created by 尹桥印 on 15/5/31.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "CFAnotationView.h"
#import "CFAnnotation.h"

@interface CFAnotationView()

@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, weak) UILabel *label;
@end

@implementation CFAnotationView

+ (instancetype)annotationViewWithMapView:(BMKMapView *)mapView
{
    static NSString *ID = @"anno";
    CFAnotationView *annotationView = (CFAnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    if (annotationView == nil) {
        // 传入循环利用标识来创建大头针控件
        annotationView = [[CFAnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
    }
    return annotationView;
}

- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        // 显示标题和子标题
        self.canShowCallout = NO;
        UIImageView *imageView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"anotation_title"]];
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor whiteColor];
        [imageView addSubview:label];
        [self addSubview:imageView];
        self.imageView = imageView;
        self.label = label;
    }
    return self;
}

- (void)setAnnotation:(CFAnnotation *)annotation{
    [super setAnnotation:annotation];
    self.image = [UIImage imageNamed:annotation.icon];
    [self.label setText:annotation.title];
    [self.label sizeToFit];
    [self.imageView setSize:CGSizeMake(self.label.width+10, self.label.height+15)];
    self.label.x = 5;
    self.label.y = 5;
    self.imageView.centerX = self.width/2;
}


-(UIImage *)getImageFromView:(UIView *)view{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
